package com.project.template.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.template.common.PageVO;
import com.project.template.common.Result;
import com.project.template.entity.Comments;
import com.project.template.entity.Posts;
import com.project.template.entity.Score;
import com.project.template.service.BannerService;
import com.project.template.service.CommentsService;
import com.project.template.service.PostsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能：
 * 作者：lhp
 */
@RestController
@RequestMapping("/posts")
public class PostsController {

    @Resource
    private PostsService postsService;
    @Resource
    private CommentsService commentsService;
    @ApiOperation(value = "列表",notes = "列表")
    @GetMapping("/list")
    private Result<List<Posts>> list(){
        return new Result().success(postsService.list());
    }
    @GetMapping("/mypage")
    public Result<PageVO<Posts>> findMyPage(
            @RequestParam Map<String, Object> query,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        Page<Posts> page= postsService.mypage(query,pageNum,pageSize);
        return new Result().success(new PageVO<>(page));
    }
    @GetMapping("/page")
    public Result<PageVO<Posts>> findPage(
            @RequestParam Map<String, Object> query,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        // 获取分页的 posts 数据
        Page<Posts> page = postsService.page(query, pageNum, pageSize);

        // 提取 posts 的 id 列表
        List<Integer> postIds = page.getRecords().stream()
                .map(Posts::getId)
                .collect(Collectors.toList());

        // 根据 posts 的 id 获取对应的 comments
        List<Comments> commentsList = commentsService.getCommentsByPostIds(postIds);
        // 将 comments 按 postId 分组
        Map<Integer, List<Comments>> commentsMap = commentsList.stream()
                .collect(Collectors.groupingBy(Comments::getPostId));

        // 将 comments 嵌套到 posts 中
        List<Posts> postsWithComments = page.getRecords().stream().map(post -> {
            post.setComments(commentsMap.getOrDefault(post.getId(), Collections.emptyList()));
            return post;
        }).collect(Collectors.toList());

        // 创建新的分页对象并返回
        Page<Posts> resultPage = new Page<>(pageNum, pageSize);
        resultPage.setRecords(postsWithComments);
        resultPage.setTotal(page.getTotal());

        return new Result().success(new PageVO<>(resultPage));
    }

    @PostMapping("/add")
//    @Validated
    public Result add(@RequestBody Posts posts){
        posts.setPostTime(DateUtil.now());
        postsService.save(posts);
        return new Result<>().success();
    }

    @PutMapping("/update")
//    @Validated
    public Result updateById(@RequestBody Posts posts){
        postsService.updateById(posts);
        return new Result<>().success();
    }

    @DeleteMapping("/delBatch")
    public Result delBatch(@RequestBody List<Integer> ids){
        postsService.removeByIds(ids);
        return new Result().success();
    }
    @PostMapping("/delete")
    public Result del(@RequestBody Map<String, Integer> request){
        if (request == null || request.get("id") == null) {
            return new Result().error("参数错误");
        }
        postsService.removeById(request.get("id"));
        return new Result().success();
    }
}
